package com.hzhc.controller.sysmanage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.RouterCollect;
import com.hzhc.entity.RouterHis;
import com.hzhc.entity.SysMenu;
import com.hzhc.entity.SysYzsMenu;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.enumBean.SystemTypeEnum;
import com.hzhc.service.RouterCollectService;
import com.hzhc.service.RouterHisService;
import com.hzhc.service.SysMenuService;
import com.hzhc.service.SysYzsMenuService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.RouterCollectVo;
import com.hzhc.vo.RouterHisVo;
import com.hzhc.vo.SysMenuVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 菜单管理
 *
 * @author wuxk
 * @since 2024-06-27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/menu")
public class RouterMenuController extends BaseController {

	private final SysYzsMenuService yzsMenuService;
	private final RouterHisService routerHisService;
	private final RouterCollectService routerCollectService;

	/**
	 * 最新常用
	 */
	@PostMapping("getRouterHis")
	public ResultUtil<List<RouterHis>> getRouterHis(HttpServletRequest request, @RequestBody RouterHisVo vo){
		UserDto user = TokenUtil.getUserMember(request);
		if(user != null){
			vo.setUserId(user.getId());
		}
		vo.setPortId(SystemTypeEnum.YZSXT.getCode());
		Page<RouterHis> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
		List<RouterHis> list = routerHisService.getList(page, vo);
		return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
	}

	/**
	 * 最新常用-保存
	 */
	@PostMapping("addRouterHis")
	public ResultUtil<Boolean> addRoutersHis(HttpServletRequest request, @RequestBody RouterHis vo){
		UserDto user = TokenUtil.getUserMember(request);
		if(user != null){
			vo.setUserId(user.getId());
		}
		vo.setPortId(SystemTypeEnum.YZSXT.getCode());
		vo.setType(0);
		return routerHisService.saveOrUpdateRet(vo);
	}

	/**
	 * 我的收藏
	 */
	@PostMapping("getRouterCollect")
	public ResultUtil<List<RouterCollect>> getRouterCollect(HttpServletRequest request, @RequestBody RouterCollectVo vo){
		UserDto user = TokenUtil.getUserMember(request);
		if(user != null){
			vo.setUserId(user.getMemberId());
		}
		vo.setPortId(SystemTypeEnum.YZSXT.getCode());
		Page<RouterCollect> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
		List<RouterCollect> list = routerCollectService.getList(page, vo);
		return ResultUtil.build(list, page.getTotal(), page.getPages(), ResultUtilCodeEnum.SUCCESS);
	}

	/**
	 * 获取菜单树
	 */
	@PostMapping("getSecondRouters")
	public ResultUtil<List<SysMenuVo>> getSecondRouters(HttpServletRequest request, @RequestBody(required = false) SysMenu datas){
		UserDto user = TokenUtil.getUserMember(request);
		List<SysMenuVo> menuListAll = new ArrayList<>();
		List<SysYzsMenu> menuListAllList = new ArrayList<>();
		if(user != null){
			menuListAll = yzsMenuService.getSecondRouters(datas.getName(),user.getId());
			menuListAllList = yzsMenuService.getMenuListAll(null);
			menuListAll = buildSecondRouters(menuListAll,menuListAllList);
			return ResultUtil.build(menuListAll, ResultUtilCodeEnum.SUCCESS);
		}
		return ResultUtil.build	(menuListAll, ResultUtilCodeEnum.SUCCESS);
	}

	/**
	 * 我的收藏-保存
	 */
	@PostMapping("addRouterCollect")
	public ResultUtil<Boolean> addRouterCollect(HttpServletRequest request, @RequestBody List<RouterCollect> list){
		UserDto user = TokenUtil.getUserMember(request);
		for (RouterCollect vo : list) {
			vo.setCreateTime(LocalDateTime.now());
			if(user != null){
				vo.setUserId(user.getId());
			}
			vo.setType(0);
			vo.setPortId(SystemTypeEnum.YZSXT.getCode());
		}
		return routerCollectService.saveOrUpdateRet(list);
	}

	/**
     * //根据parentId 递归拼接父级的菜单URL
	 * @param menuListAll
     * @return
	 */
	public List<SysMenuVo> buildSecondRouters(List<SysMenuVo> menuListAll,List<SysYzsMenu> menuListAllList){
		//根据parentId 递归拼接父级的菜单URL
		for (SysYzsMenu menu : menuListAllList) {
			if(menu.getParentId() != null && menu.getParentId() != 0){
				SysYzsMenu parentMenu = null;
				final Optional<SysYzsMenu> first = menuListAllList.stream().filter(m -> Objects.equals(m.getId(), menu.getParentId())).findFirst();
				if(first.isPresent()){
					parentMenu = first.get();
				}
				if( menu.getType()==1){
					if(parentMenu != null){
						menu.setUrl(parentMenu.getUrl() +"/"+ menu.getUrl());
					}
				}else{
					if(parentMenu != null) {
						menu.setUrl(parentMenu.getUrl() + menu.getUrl());
					}
				}
			}
			for (SysMenuVo vo : menuListAll) {
				if(Objects.equals(vo.getId(), menu.getId())){
					vo.setUrl(menu.getUrl());
					break;
				}
			}
		}
		return menuListAll;
	}
}
